/*
 * @Author: fumin 31408026@qq.com
 * @Date: 2025-11-07 15:57:36
 * @LastEditors: fumin 31408026@qq.com
 * @LastEditTime: 2025-11-10 16:19:26
 * @FilePath: /xinjiangkf-miniapp/src/store/user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
    state: () => ({
        phone: '',
        openid: '',
        token: '',
        isLoggedIn: false,
        isVerified: false,
        balance: 0,
        usedData: 0,
        usedCharge: 0,
        voiceBalance: 0,
        points: 0,
        membershipLevel: 0
    }),

    getters: {
        maskedPhone: (state) => {
            if (!state.phone || state.phone.length !== 11) return state.phone
            return state.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
        },
        formattedBalance: (state) => state.balance.toFixed(2)
    },

    actions: {
        // 微信登录
        async login(code) {
            try {
                const { api } = await
                import ('@/utils/api')
                const res = await api.wxLogin({ code })
                if (res.code === 0) {
                    this.openid = res.data.openid
                    this.token = res.data.token
                    this.isLoggedIn = true
                        // 保存到本地存储
                    uni.setStorageSync('userInfo', {
                            openid: res.data.openid,
                            token: res.data.token
                        })
                        // 更新header中的openid
                    const { request } = await
                    import ('@/utils/api')
                    request.defaults = request.defaults || {}
                    request.defaults.headers = request.defaults.headers || {}
                    request.defaults.headers.openid = res.data.openid
                }
                return res
            } catch (error) {
                console.error('登录失败:', error)
                throw error
            }
        },

        // 设置用户信息
        setUserInfo(info) {
            this.phone = info.phone || this.phone
            this.balance = info.balance || this.balance
            this.usedData = info.usedData || this.usedData
            this.usedCharge = info.usedCharge || this.usedCharge
            this.voiceBalance = info.voiceBalance || this.voiceBalance
            this.points = info.points || this.points
            this.membershipLevel = info.membershipLevel || this.membershipLevel
        },

        // 退出登录
        logout() {
            this.$reset()
            uni.removeStorageSync('userInfo')
        },

        // 更新余额
        updateBalance(amount) {
            this.balance = amount
        },

        // 更新数据
        updateData(data) {
            this.usedData = data.usedData || this.usedData
            this.usedCharge = data.usedCharge || this.usedCharge
            this.voiceBalance = data.voiceBalance || this.voiceBalance
            this.points = data.points || this.points
        }
    }
})